<?php

require_once './library/XML/xmlToObjArray.php';
require_once './application/models/Tables/cache.php';
require_once './application/models/Objets/Composant.php';
require_once './application/core/Client/visualisationSoap.php';

class lanceRechercheCore extends IndexCore
{

    private $_composant;
    private $_tmp;
    private $_ip;
    private $_mask;
    private $_once;

    public function lanceRecherche()
    {
        $this->_ip = $this->_controller->getRequest()->getParam('ip');
        $this->_mask = $this->_controller->getRequest()->getParam('mask');
        $params = array('Ip' => $this->_ip,
            'Mask' => $this->_mask);
        $c = new visualisationSoap($this->_controller);
        $c->lanceRecherche($params, 'lanceRechercheRecolteResult');
        $this->MiseEnAttenteReponse(true, '/visualisation/lancerechercheresult', null);
    }

    private function parseLanceRechercheResp($xml)
    {
        $obj = new xmlToObjArray();
        $xml = $obj->parse_xml_data($xml, false);
        $val = array();
        $val[0] = $val[2] = $val[3] = $val[4] = "&#216";
        if (isset($xml[0]['PARAM'][0]['COMPOSANT'][0]['SYSTEMENAME'][0][0]))
            $val[0] = array($xml[0]['PARAM'][0]['COMPOSANT'][0]['SYSTEMENAME'][0][0]);
        if (isset($xml[0]['PARAM'][0]['COMPOSANT'][0]['HOST'][0][0]))
            $val[1] = array($xml[0]['PARAM'][0]['COMPOSANT'][0]['HOST'][0][0]);
        if (isset($xml[0]['PARAM'][0]['COMPOSANT'][0]['SYSTEMEDESCRIPTION'][0][0]))
            $val[2] = array($xml[0]['PARAM'][0]['COMPOSANT'][0]['SYSTEMEDESCRIPTION'][0][0]);
        if (isset($xml[0]['PARAM'][0]['COMPOSANT'][0]['NBINTERFACE'][0][0]))
            $val[3] = array($xml[0]['PARAM'][0]['COMPOSANT'][0]['NBINTERFACE'][0][0]);
        if (isset($xml[0]['PARAM'][0]['COMPOSANT'][0]['SYSTEMEUPTIME'][0][0]))
            $val[4] = array($xml[0]['PARAM'][0]['COMPOSANT'][0]['SYSTEMEUPTIME'][0][0]);
        if (isset($xml[0]['PARAM'][0]['COMPOSANT'][0]['LISTEIP_LIENS'][0][0]))
        {
            $val[5] = $xml[0]['PARAM'][0]['COMPOSANT'][0]['LISTEIP_LIENS'][0][0];
            $this->_tmp = array("{", "}", "[", "]");
            $val[5] = str_replace($this->_tmp, "", $val[5]);
        }
        return $val;
    }

    public function lanceRechercheResult()
    {
        $this->_once = $this->_controller->getRequest()->getParam('o');
        if ($this->_once != 'true')
            $this->_once = false;
        $cache = new cache();
        $rst = $cache->getInfo($this->_controller->_idClient, 'lanceRechercheRecolteResult');
        if ($rst != false)
        {
            $result = new rechercheVisualisation();
            $result->rechercheTermine = new RechercheTermine();
            foreach ($rst as $key => $val)
            {
                $val = $this->parseLanceRechercheResp($rst[$key]['Xml']);
                if (isset($val[1]))
                {
                    $result->rechercheTermine->termine = false;
                    $this->_composant = $this->objectComposant($val);
                    $result->donnees[] = $this->_composant;
                    $this->putComposantBase($val);
                }
                if (!isset($val[1]))
                    $result->rechercheTermine->termine = true;
                $cache->deleteInfo($this->_controller->_idClient, 'lanceRechercheRecolteResult', $rst[$key]['Id']);
            }
            $this->MiseEnAttenteReponse(false, '/visualisation/lancerechercheresult', $result);
        }
        else
        {
            if ($this->_once != true)
                $this->MiseEnAttenteReponse(true, '/visualisation/lancerechercheresult', null);
            else
                $this->MiseEnAttenteReponse(false, '/visualisation/lancerechercheresult', 'once');
        }
    }

    public function stopRecherche()
    {
        $params = array('Param' => 'stop',
            'ID' => $this->_controller->_idClient);
        $c = new visualisationSoap($this->_controller);
        $c->stopRecherche($params, 'lanceRechercheRecolteResult');
        $this->MiseEnAttenteReponse(false, '/visualisation/lancerechercheresult', false);
    }

    private function putComposantBase($val)
    {
        $obj = new ObjectComposant();
        $check = $obj->rechercheComposant($val[1][0]);
        if ($check != false)
            $obj->modifierComposant($check[0]['IdComposant'], $val[0][0], $val[1][0], $val[3][0], $val[4][0], $val[2][0], '');
        else
            $obj->ajouterComposant($val[0][0], $val[1][0], $val[3][0], $val[4][0], $val[2][0], '');
        return true;
    }

    private function objectComposant($val)
    {
        $this->_composant = new Composant();
        $this->_composant->id = uniqid();
        $this->_composant->host = $val[0];
        $this->_composant->ip = $val[1];
        $this->_composant->model = $val[2];
        $this->_composant->port = $val[3];
        $this->_composant->uptime = $val[4];
        $this->_composant->link = $val[5];
        return $this->_composant;
    }

}
?>
